package org.poker.model;

import java.io.Serializable;
import java.util.Comparator;

public class Card extends Suit implements Comparable<Card>, Comparator<Card>, Serializable {

    private static final long serialVersionUID = -3367468502939208973L;

    private int id;

    private int number;

    private boolean selected;

    public final static int CARD_A = 1;
    public final static int CARD_2 = 2;
    public final static int CARD_3 = 3;
    public final static int CARD_4 = 4;
    public final static int CARD_5 = 5;
    public final static int CARD_6 = 6;
    public final static int CARD_7 = 7;
    public final static int CARD_8 = 8;
    public final static int CARD_9 = 9;
    public final static int CARD_10 = 10;
    public final static int CARD_J = 11;
    public final static int CARD_Q = 12;
    public final static int CARD_K = 13;
    public final static int CARD_AA = 14;


    public Card() {
    }

    public Card(int numero, int naipe, int id) {
	super(naipe);
	this.number = numero;
	this.id = id;
	this.selected = false;
    }

    public Card(int numero, int naipe) {
	super(naipe);
	this.number = numero;
	this.selected = false;
    }

    public Card(int numero) {
	this.number = numero;
	this.selected = false;
    }

    public int getNumber() {
	return this.number;
    }

    public void setNumber(int number) {
	this.number = number;
    }

    public int getId() {
	return this.id;
    }

    public void setId(int id) {
	this.id = id;
    }

    public void selectUnselect() {
	this.selected = !this.selected;
    }

    public boolean isSelected() {
	return this.selected;
    }

    public void setSelected(boolean selected) {
	this.selected = selected;
    }

    @Override
    public String toString() {
	String naipe = " ".concat(super.toString());
	String retorno = "";
	if(this.number == Card.CARD_A)
	    retorno = "A".concat(naipe);
	else if(this.number == Card.CARD_AA)
	    retorno = "A+".concat(naipe);
	else if(this.number >= Card.CARD_2 && this.number <= Card.CARD_10)
	    retorno = String.valueOf(this.number).concat(naipe);
	else if(this.number == Card.CARD_J)
	    retorno = "J".concat(naipe);
	else if(this.number == Card.CARD_Q)
	    retorno = "Q".concat(naipe);
	else if(this.number == Card.CARD_K)
	    retorno = "K".concat(naipe);

	if(this.selected)
	    return "(#)".replaceFirst("#", retorno);
	return " # ".replaceFirst("#", retorno);
    }

    public int compare(Card carta1, Card carta2) {
	return Integer.valueOf(carta1.getNumber()).compareTo(Integer.valueOf(carta2.getNumber()));
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + this.number;
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	final Card other = (Card) obj;
	if (this.number != other.number || super.getNaipe() != other.getNaipe())
	    return false;
	return true;
    }

    public int compareTo(Card o) {
	if (this.number > o.number)
	    return 1;
	if (this.number < o.number)
	    return -1;
	if(super.getNaipe() > super.getNaipe())
	    return 1;
	if(super.getNaipe() < super.getNaipe())
	    return -1;
	return 0;
    }
}
